package com.example.plugins.tutorial.confluence;

import org.apache.http.client.methods.HttpPost;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.util.Collection;
import java.util.Iterator;

/**
 * Created with IntelliJ IDEA.
 * User: nkozlov
 * Date: 28.09.12
 * Time: 16:47
 * To change this template use File | Settings | File Templates.
 */
public class DisplayRepositoryTree {



    private void listEntries(SVNRepository repository, String path, StringBuilder builder) throws SVNException {

        long latestRevision = repository.getLatestRevision();

        Collection entries = repository.getDir(path, latestRevision, null, (Collection) null);
        Iterator iterator = entries.iterator();
        while (iterator.hasNext()) {
            SVNDirEntry entry = (SVNDirEntry) iterator.next();
            //.append(" ( author: '").append(entry.getAuthor()).append("'; revision: ").append(entry.getRevision()).append("; date: ").append(entry.getDate()).append(")")
            builder.append("<p>").append("/").append(path.equals("") ? "" : path + "/").append(entry.getName()).append("</p>");
           /* if (entry.getKind() == SVNNodeKind.DIR) {
                listEntries(repository, ((path.equals("")) ? entry.getName() : path + "/" + entry.getName()), builder);
            }*/
        }
    }

    public void launch(StringBuilder builder) throws SVNException {
        DAVRepositoryFactory.setup();
        String url = "http://svn.bpm.lanit/AUTO";
        String name = "NKozlov";
        String password = "Koznik#22965";
        //HttpClient httpClient =  new

        HttpPost httpPost = new HttpPost(url);


        SVNRepository repository = null;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIDecoded(url));
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
            repository.setAuthenticationManager(authManager);
            /*System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
            System.out.println("Repository UUID: " + repository.getRepositoryUUID(true));*/

            SVNNodeKind nodeKind = repository.checkPath("", -1);
            if (nodeKind == SVNNodeKind.NONE) {
                System.err.println("There is no entry at '" + url + "'.");
                System.exit(1);
            } else if (nodeKind == SVNNodeKind.FILE) {
                System.err.println("The entry at '" + url + "' is a file while a directory was expected.");
                System.exit(1);
            }

            listEntries(repository, "", builder);

            /* long latestRevision = repository.getLatestRevision();
       System.out.println("Repository latest revision: " + latestRevision);*/
        } catch (
                SVNException svne
                )

        {
            //handle exception
        }
    }
}
